﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataModel;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MetaDesigner
{    
    /// <summary>
    /// Interaction logic for TermLabelAttributeManager.xaml
    /// </summary>
    public partial class TermLabelAttributeManager : Window
    {
        Term t;
        delegate void NoArgDelegate();

        /// <summary>
        /// Initializes a new instance of the <see cref="TermLabelAttributeManager"/> class.
        /// </summary>
        /// <param name="t">The t.</param>
        public TermLabelAttributeManager(Term t)
        {
            InitializeComponent();
            this.t = t;

            UpdateDataGrids();
        }

        /// <summary>
        /// Updates the data grids item sources.
        /// </summary>
        public void UpdateDataGrids()
        {
            SharedPropertiesDataGrid.ItemsSource = t.SharedCustomProperties;
            LocalPropertiesDataGrid.ItemsSource = t.LocalCustomProperties;
            LabelsDataGrid.ItemsSource = t.Labels;
            cultureCombo.ItemsSource = TermLabel.CultureCodes;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// Handles the Closing event of the Window control. Checks for duplicate keys.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="CancelEventArgs"/> instance containing the event data.</param>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            bool dupes = false;
            string msg = "";
                          
            if (t.SharedCustomProperties.HasDupes)
            {
                dupes = true;
                msg += "One or more keys in the shared properties are duplicated. Please ensure there are no duplicated Keys and try again.";
            }

            if (t.LocalCustomProperties.HasDupes)
            {
                if (dupes)
                {
                    msg += "\n";
                }
                else
                {
                    dupes = true;
                }
                msg += "One or more keys in the local are duplicated. Please ensure there are no duplicated Keys and try again.";
            }
            

            if (dupes)
            {
                e.Cancel = true;
                MessageBox.Show(msg);
            }
            else
            {
                SharedPropertiesDataGrid.ItemsSource = null;
                LocalPropertiesDataGrid.ItemsSource = null;
                LabelsDataGrid.ItemsSource = null;
                cultureCombo.ItemsSource = null;

                /* SharedPropertiesDataGrid.CommitEdit();
            LocalPropertiesDataGrid.CommitEdit();
            LabelsDataGrid.CommitEdit(); */
            }
        }
    }
}
